cmake_minimum_required(VERSION 3.10)
project(libsecrypto C)
include_directories(include)

add_compile_options(-Wall -pedantic -fno-builtin -std=c99 -fno-stack-protector -O2)

# 交叉编译时启用
# 设置库文件搜索根目 (交叉编译的链接库位于/usr/arm-linux-gnueabihf下)
#set(CMAKE_FIND_ROOT_PATH /usr/arm-linux-gnueabihf)
#set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
#set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
#set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
#set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
#set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
#set(CMAKE_C_COMPILER /home/january/Desktop/optee-project/toolchains/aarch64/bin/aarch64-none-linux-gnu-gcc)
#set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)

FILE(GLOB FP src/fp/*.c)
FILE(GLOB CURVES src/curves/*.c)
FILE(GLOB NN src/nn/*.c)
FILE(GLOB SIG src/sig/*.c)
FILE(GLOB HASH src/hash/*.c)
FILE(GLOB UTILS src/utils/*.c)
FILE(GLOB CIPHER src/cipher/*.c)
FILE(GLOB ENCODE src/encode/*.c)

# 椭圆曲线密码库
add_library(ecc
            ${FP}
            ${CURVES}
            ${HASH}
            ${NN}
            ${SIG}
            ${UTILS}
            ${ENCODE}
            ${CIPHER}
            src/rand/rand.c
        )

# 椭圆曲线运算库
add_library(ec
            ${FP}
            ${CURVES}
            ${NN}
            ${UTILS})

# 有限域大整数运算库
add_library(arith
            ${FP}
            ${NN}
            ${UTILS})

add_executable(cipher_test
            src/tests/cipher_test.c
            src/log/log.c
            ${CIPHER}
            src/utils/utils.c
        )
if(NOT ${CMAKE_C_COMPILER} MATCHES .*arm-linux.* )
# 密钥生成工具
add_executable(keytool
        src/tools/keytool.c
        src/log/log.c
        src/rand/rand.c
        src/encode/base64.c)
target_link_libraries(keytool ecc)

# sm3 digest tool
add_executable(sm3sum
        src/tools/sm3sum.c
        src/log/log.c)
target_link_libraries(sm3sum ecc)

# 测试程序
add_executable(ecc_test
        src/tests/ecc_test.c
        src/rand/rand.c
        src/log/log.c
        src/encode/base64.c)
# just for x86 compilation
find_library(OPENSSL crypto)
target_link_libraries(ecc_test ecc ${OPENSSL})
endif()